//
//  TabBarVC.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/7/3.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "TabBarVC.h"
#import "UserModal.h"
#import "StoreViewController.h"
#include "BrokerManageVc.h"
#import "DealerMineVc.h"
#import "MainViewController.h"
#import "UIImage+Category.h"
#import "IWNavigationController.h"
#import "AppDelegate.h"
#import "MyOrderViewPageVc.h"
#import "MyCutomerVc.h"
#import "MiddleManMyVC.h"
#import "NSString+category.h"
#import "MessageDM.h"
#import "config.h"



@interface TabBarVC ()<UITabBarControllerDelegate>


@end

@implementation TabBarVC
@synthesize nfc,nsc,ntc,nforec;



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:UIColorFromRGB(0xdb2a2b),                                                                                                              NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    self.tabBar.hidden = NO;
    
    
   
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 49)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.tabBar insertSubview:whiteView atIndex:0];
    self.tabBar.opaque = YES;
    
    [self loadviewController];
    
    self.delegate = self;
    
    [self loadCustomTabBar];


}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    UIViewController *vc = [self.viewControllers objectAtIndex:3];
    NSInteger count = [MessageDM getMessageNotReadCount];
    if (count==0) {
        vc.tabBarItem.badgeValue = nil;
    }else{
        vc.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld",(long)count];
    }
}

-(void)loadviewController
{
    
    if ([UserModal shareUserModel].userType == UserTypeTrader) {

        MainViewController *fc = [[MainViewController alloc]init];
        nfc = [[UINavigationController alloc]initWithRootViewController:fc];
        
        StoreViewController *sc = [[StoreViewController alloc]init];
        sc.isHidden = YES;
        nsc = [[UINavigationController alloc]initWithRootViewController:sc];
        
        BrokerManageVc *tc = [[BrokerManageVc alloc]init];
        tc.isHidden = YES;
        ntc = [[UINavigationController alloc]initWithRootViewController:tc];
        
        DealerMineVc *foreC = [[DealerMineVc alloc]init];
        nforec= [[UINavigationController alloc]initWithRootViewController:foreC];
        
        UIImage * normalImage = [[UIImage imageNamed:@"icon6-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage * selectImage = [[UIImage imageNamed:@"icon5-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UITabBarItem * tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:normalImage selectedImage:selectImage];
       
        fc.tabBarItem = tabBarItem;
        
        normalImage = [[UIImage imageNamed:@"icon7-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        selectImage = [[UIImage imageNamed:@"icon8-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabBarItem = [[UITabBarItem alloc]initWithTitle:@"门店" image:normalImage selectedImage:selectImage];
        sc.tabBarItem = tabBarItem;
        
        
        normalImage = [[UIImage imageNamed:@"icon9-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        selectImage = [[UIImage imageNamed:@"icon10-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabBarItem = [[UITabBarItem alloc]initWithTitle:@"经纪人" image:normalImage selectedImage:selectImage];
        ntc.tabBarItem = tabBarItem;
        
        
        
        normalImage = [[UIImage imageNamed:@"icon11-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        selectImage = [[UIImage imageNamed:@"icon12-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:normalImage selectedImage:selectImage];
        foreC.tabBarItem = tabBarItem;
        
        
        NSArray *array =[[NSArray alloc]initWithObjects:nfc,nsc,ntc,nforec,nil];
        
        
        [self setViewControllers:array animated:YES];
        
        
    }else{
        
        MainViewController *fc = [[MainViewController alloc]init];
        nfc = [[UINavigationController alloc]initWithRootViewController:fc];
        
        MyOrderViewPageVc *sc = [[MyOrderViewPageVc alloc]init];
        [sc.navigationController.navigationBar setTintColor:[UIColor grayColor]];
        sc.isHidden = YES;
        nsc = [[UINavigationController alloc]initWithRootViewController:sc];
        
        MyCutomerVc *tc = [[MyCutomerVc alloc]init];
        tc.isHidden = YES;
        ntc = [[UINavigationController alloc]initWithRootViewController:tc];
        
        MiddleManMyVC *foreC = [[MiddleManMyVC alloc]init];
        nforec= [[UINavigationController alloc]initWithRootViewController:foreC];
        
        
        UIImage * normalImage = [[UIImage imageNamed:@"icon6-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage * selectImage = [[UIImage imageNamed:@"icon5-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UITabBarItem * tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:normalImage selectedImage:selectImage];
        fc.tabBarItem = tabBarItem;
        
        
        normalImage = [[UIImage imageNamed:@"icon7-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        selectImage = [[UIImage imageNamed:@"icon8-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabBarItem = [[UITabBarItem alloc]initWithTitle:@"订单" image:normalImage selectedImage:selectImage];
        sc.tabBarItem = tabBarItem;
        
        
        normalImage = [[UIImage imageNamed:@"icon9-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        selectImage = [[UIImage imageNamed:@"icon10-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabBarItem = [[UITabBarItem alloc]initWithTitle:@"客户" image:normalImage selectedImage:selectImage];
        ntc.tabBarItem = tabBarItem;
        
        
        
        normalImage = [[UIImage imageNamed:@"icon11-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        selectImage = [[UIImage imageNamed:@"icon12-1"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:normalImage selectedImage:selectImage];
        foreC.tabBarItem = tabBarItem;
        
        
        NSArray *array =[[NSArray alloc]initWithObjects:nfc,nsc,ntc,nforec,nil];
        
        
        [self setViewControllers:array animated:YES];
        
    }
    
}



-(void)loadCustomTabBar
{
    //设置tabbar的背景图片
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"main_13"]];
    img.frame = CGRectMake(0, 0,self.tabBar.frame.size.width,self.tabBar.frame.size.height);
    img.contentMode = UIViewContentModeScaleToFill;
    [self.tabBar insertSubview:img atIndex:1];
    
    
    
    if (true) {
    
        UITabBar *tabBar = self.tabBar;
        UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
        UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
        UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
        UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
        
        
        UIColor *norColor = UIColorFromRGB(0x333333);
        
        [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             norColor, NSForegroundColorAttributeName,
                                             nil] forState:UIControlStateNormal];
        [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             norColor, NSForegroundColorAttributeName,
                                             nil] forState:UIControlStateNormal];
        [tabBarItem3 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             norColor, NSForegroundColorAttributeName,
                                             nil] forState:UIControlStateNormal];
        [tabBarItem4 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             norColor, NSForegroundColorAttributeName,
                                             nil] forState:UIControlStateNormal];
        
        
        UIColor *seColor = [UIColor redColor];
        
        [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             seColor, NSForegroundColorAttributeName,
                                             nil] forState:UIControlStateSelected];
        [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             seColor, NSForegroundColorAttributeName,
                                             nil] forState:UIControlStateSelected];
        [tabBarItem3 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             seColor, NSForegroundColorAttributeName,
                                             nil] forState:UIControlStateSelected];
        [tabBarItem4 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             seColor, NSForegroundColorAttributeName,
                                             nil] forState:UIControlStateSelected];


    }

}






-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
}





- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //取消底部提醒数字
    viewController.tabBarItem.badgeValue = nil;
}




@end
